package graphics.screens;

import graphics.Background;
import graphics.BootColor;
import graphics.MultiLineText;

import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import player.MP3Metadata;
import state.State;
import tool.KeyCode;
import tool.ScreenUtils;
import boot.Boot;

public class MainScreen extends Canvas implements CommandListener {

	private Graphics graphics;

	private final Command pauseCommand = new Command("Pause", Command.ITEM, 1);
	private final Command playCommand = new Command("Play", Command.ITEM, 1);
	private final Command AuthorCommand = new Command("Author", Command.EXIT, 1);
	// private final Command titleCommand = new Command("Title", Command.EXIT,
	// 1);

	private Hashtable displayedCommand = new Hashtable();
	private Timer alertTimer;
	private MultiLineText text = null;
	private MultiLineText alert = null;
	private Background background = null;
	private MP3Metadata metadata;
	private boolean mainButtonPushed = false;

	public MainScreen() throws Throwable {
		setCommandListener(this);
		text = new MultiLineText(0, graphics);
		background = new Background();
	}

	protected void paint(Graphics g) {
		graphics = g;

		ScreenUtils.setBlackBackground(graphics);
		background.repaint(graphics);

		graphics.setColor(BootColor.BLACK);
		graphics.drawRect(0, 0, 240 - 6, 27);

		if (alert != null) {
			drawAlert();
		} else if (metadata != null) {
			drawCurrentMetadata();
		}
	}

	private void drawAlert() {
		if (alert == null) {
			return;
		}
		alert.repaint(graphics, BootColor.BASIC_MENU_COLOR);
	}

	public void keyPressed(int keyCode) {

		if ((mainButtonPushed) && (keyCode == KeyCode.ASTERISK)) {
			Boot.getInstance().lockScreen();
			Boot.state().remove(State.SEARCH_ENGAGED);
		} else {
			Boot.getInstance().keyPressed(keyCode);
		}

		Boot.repaint();
	}

	public void drawCurrentMetadata() {
		if (Boot.state().equals(State.AUTHOR_DISPLAY)) {
			text.setContent("by " + metadata.getAuthor());
			text.repaint(graphics, BootColor.FLASHY_MENU_COLOR);
		} else {
			text.setContent(metadata.getTitle());
			int color = BootColor.WHITE;
			// if (Boot.state().equals(State.GOLDEN_TUNE)) {
			// color = BootColor.GOLDEN;
			// }
			text.repaint(graphics, color);
		}
	}

	public void alert(String message, int timeout) {
		alert = new MultiLineText(0, graphics);
		alert.setContent(message);
		repaint();

		if (alertTimer != null) {
			alertTimer.cancel();
		}
		alertTimer = new Timer();
		alertTimer.schedule(new TimerTask() {
			public void run() {

				alert = null;
				alertTimer = null;
				repaint();
			}
		}, timeout);
	}

	public void commandAction(Command command, Displayable dis) {

		if (command == playCommand) {
			if (mainButtonPushed) {
				Boot.getInstance().playCurrentFromStart();
			} else {
				Boot.getInstance().playPlayer();
			}
			onMainButtonPushed();
		} else if (command == pauseCommand) {
			onMainButtonPushed();
			Boot.getInstance().pausePlayer();
		} else if (command == AuthorCommand) {
			Boot.addState(State.AUTHOR_DISPLAY);
			new Timer().schedule(new TimerTask() {

				public void run() {
					Boot.removeState(State.AUTHOR_DISPLAY);
					handleCommands();
					repaint();
				}
			}, 3000);
		}
		handleCommands();
		repaint();
	}

	private void onMainButtonPushed() {
		mainButtonPushed = true;
		new Timer().schedule(new TimerTask() {

			public void run() {
				mainButtonPushed = false;
			}
		}, 1000);
	}

	private void secureAddCommand(Command command) {
		if (displayedCommand.get(command) == null) {
			displayedCommand.put(command, "");
			addCommand(command);
		}
	}

	private void secureRemoveCommand(Command command) {
		if (displayedCommand.get(command) != null) {
			displayedCommand.remove(command);
			removeCommand(command);
		}
	}

	public void handleCommands() {

		secureRemoveCommand(playCommand);
		secureRemoveCommand(pauseCommand);
		secureRemoveCommand(AuthorCommand);

		if (Boot.state().equals(State.PLAY_MP3)) {
			secureAddCommand(pauseCommand);
		} else if (Boot.state().equals(State.PAUSE_MP3)) {
			secureAddCommand(playCommand);
		}

		if ((Boot.state().equals(State.PLAY_MP3))
				|| (Boot.state().equals(State.PAUSE_MP3))) {
			if (Boot.state().equals(State.AUTHOR_DISPLAY)) {
				// secureAddCommand(titleCommand);
			} else {
				secureAddCommand(AuthorCommand);
			}
		}
	}

	public void setMetadata(MP3Metadata someMetadata) {
		metadata = someMetadata;
	}
}
